//
//  MessageTableViewCell.m
//  HAODE
//
//  Created by LiYinDi on 15/12/29.
//  Copyright © 2015年 reabam.com. All rights reserved.
//

#import "MessageTableViewCell.h"

@implementation MessageTableViewCell

- (void)awakeFromNib {
    // Initialization code
    [self setAutoLayout];
}
-(void)setModel:(MessageModel *)model
{
    NSString *timeStr=model.msgDate;
    NSString *curStr=[self GetCurTime];
    double interval=[self GetStringTimeDiff:timeStr timeE:curStr];
    int hour=interval/3600;
    int minute=(int)interval % 3600 / 60;
    int day=(int)interval/3600/24;
    //    NSLog(@"curStr==%@,leftTimeStr===%@",curStr,leftTimeStr);
    //    NSLog(@"%f",interval);
    //    NSLog(@"hour===%d,minute=%d,day==%d",hour,minute,day);
    if (day!=0&&day>0) {
        _timeLabel.text=[NSString stringWithFormat:@"%d天前",day];
    }
    else if((day==0||day<0)&&hour>0)
    {
        _timeLabel.text=[NSString stringWithFormat:@"%d小时前",hour];
    }
    else if ((day<=0&&hour<=0)&&minute>=0) {
        _timeLabel.text=[NSString stringWithFormat:@"%d分前",minute];
    }
    
    
    _contentLabel.text= model.msgIntro;

}
-(void)setAutoLayout
{
    [_iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).with.offset(12);
        make.width.equalTo(@45);
        
         make.height.equalTo(@45);
        
        make.centerY.equalTo(self.mas_centerY);
    }];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_iconImageView.mas_right).with.offset(12);
        make.right.equalTo(_timeLabel.mas_left).with.offset(-5);
        
        make.height.equalTo(@21);
        
        make.top.equalTo(_iconImageView.mas_top) ;
    }];
    [_timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
      // make.left.equalTo(_titleLabel.mas_right).with.offset(5);
        make.right.equalTo(self.mas_right).with.offset(-19);
        
        make.height.equalTo(@16);
        
        make.centerY.equalTo(_titleLabel.mas_centerY);
    }];
    [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_iconImageView.mas_right).with.offset(12);
        make.right.equalTo(self.mas_right) ;
        
        make.height.equalTo(@21);
        
        make.bottom.equalTo(_iconImageView.mas_bottom) ;
    }];
    [_lineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).with.offset(10);
          make.right.equalTo(self.mas_right).with.offset(-10) ;
        
        make.height.equalTo(@1);
        
        make.bottom.equalTo(self.mas_bottom).with.offset(-1);
    }];

    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
- (double)GetStringTimeDiff:(NSString*)timeS timeE:(NSString*)timeE

{
    
    double timeDiff = 0.0;
    
    NSDateFormatter *formatters = [[NSDateFormatter alloc] init];
    [formatters setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *dateS = [formatters dateFromString:timeS];
    
    
    
    NSDateFormatter *formatterE = [[NSDateFormatter alloc] init];
    [formatterE setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *dateE = [formatterE dateFromString:timeE];
    
    
    timeDiff = [dateE timeIntervalSinceDate:dateS ];
    
    return timeDiff;
    
}

- (NSString*)GetCurTime

{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    NSString*timeString=[formatter stringFromDate: [NSDate date]];
    
    
    return timeString;
}

@end
